package com.javadaemon.Tinderbox.gui;

import com.javadaemon.Tinderbox.Constants;
import com.javadaemon.Tinderbox.Tinderbox;
import com.javadaemon.Tinderbox.model.Misc;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * This file is part of Tinderbox.
 *
 * Tinderbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ponix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tinderbox.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This is the control panel that appears when pressing F9.
 * @author Mads Horndrup
 */
public class ControlPanel extends JFrame {

    private boolean isHiding = false;
    private Tinderbox parent;
    private JMenuBar menu;

    public ControlPanel(Tinderbox parent) {
        // Initialize the frame.
        this.parent = parent;
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                destroy();
            }
        });
        this.setTitle("Tinderbox - Tools");
        this.setIconImage(Tinderbox.getImage());
        this.setLayout(new BorderLayout());

        // Menu bar.
        menu = new MenuBar(this);
        this.setJMenuBar(menu);

        // The buttons, for the first row.
        JButton screenshotButton = new JButton("Screenshot (F11)");
        screenshotButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Misc.createScreenshot();
            }
        });
        JButton updatesButton = new JButton("Check for updates");
        updatesButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                double newVersion = Misc.getVersion();
                String versionString;
                if (newVersion > Constants.VERSION) {
                    versionString = "There is a new update available from the website!";
                } else {
                    versionString = "You have the newest version!";
                }
                JOptionPane.showMessageDialog(new JFrame(), ""+versionString);
            }
        });
        screenshotButton.setPreferredSize(new Dimension(130, 30));
        updatesButton.setPreferredSize(new Dimension(130, 30));


        // Pane ordering the first row of buttons.
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        buttonPanel.add(screenshotButton);
        buttonPanel.add(updatesButton);

        // Label saying Tinderbox.
        JLabel nameLabel = new JLabel("Tinderbox - Powered by JavaDaemon");
        nameLabel.setPreferredSize(new Dimension(180, 10));

        // Pane for the above label.
        JPanel labelPanel = new JPanel();
        labelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        labelPanel.add(nameLabel);

        // Panel holding actual content.
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(2, 1, 5, 5));
        contentPane.add(buttonPanel);
        contentPane.add(labelPanel);

        // The rootpanel.
        JPanel rootPane = new JPanel();
        rootPane.setLayout(new BorderLayout(10, 10));
        rootPane.add(contentPane, BorderLayout.CENTER);

        this.setContentPane(rootPane);

        // Pack and be visible.
        this.pack();
        this.setVisible(true);
    }

    public void hide(boolean status) {
        isHiding = status;
    }

    public void toggleHide() {
        if (!isHiding) {
            this.setVisible(false);
            hide(true);
        } else {
            this.setVisible(true);
            hide(false);
        }
    }

    public void destroy() {
        this.setVisible(false);
        hide(true);
        parent.destroyControlPanel();
    }
}
